﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SQLite;
using Com.Toook.Common;

public partial class nst_joke_JokeEditPublish : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            InitJoke();
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string sql = "update JokePublish set title=@title, content=@content, type=@type " +
            " where id=" + lblId.Text;
        SQLiteConnection con = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, con);
        cmd.Parameters.Add("@title", DbType.String, 30).Value = txtTitle.Text;
        cmd.Parameters.Add("@content", DbType.String).Value = txtContent.Text;
        cmd.Parameters.Add("@type", DbType.Int32).Value = ddlType.SelectedValue;

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            txtContent.Text = "修改成功";
        }
        catch(Exception ex)
        {
            LogHelper.Log.Error(ex);
        }
        finally
        {
            SqliteHelper.CloseDbResource(con, null);
        }
    }
    private void InitJoke()
    {
        string type = "";
        string sql = "select title, content, type " +
            " from JokePublish where id=" + Request["id"];//SQLiteDatabase
        SQLiteConnection con = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, con);
        SQLiteDataReader reader = null;
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            if(reader.Read())
            {
                txtContent.Text = reader["content"].ToString();
                txtTitle.Text = reader["title"].ToString();
                type = reader["type"].ToString();
                lblId.Text = Request["id"];
            }
        }
        catch(Exception ex)
        {
            LogHelper.Log.Error(ex);
        }
        finally
        {
            SqliteHelper.CloseDbResource(con, reader);
        }
        InitDdlType(type);
    }
    private void InitDdlType(string type)
    {
        foreach(ListItem item in ddlType.Items)
        {
            if(type == item.Value)
            {
                item.Selected = true;
                return;
            }
        }
    }
}
